package com.hust.ele.controller;

import java.util.Date;
import java.util.HashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hust.ele.bean.Test;
import com.hust.ele.component.MailComponent;
import com.hust.ele.service.TestService;

/**
 * @description 测试类controller
 * @author 周杨
 * @Time 2018年10月24日 下午2:13:36
 */
@Controller//如果要访问静态模板文件 不要使用restController
@RequestMapping(value = "/test")
public class TestController {
	
	private static final Logger logger = LoggerFactory.getLogger(TestController.class);
	
	@Autowired
	private TestService testService;
	@Autowired
	private MailComponent mailComponent;
	
	@RequestMapping(value="index")
	public @ResponseBody String index() {
		logger.debug("index,this is a log test, debug");
		logger.info("index,this is a log test, info");
		return "hello world";
	}
	
	/**
	 * @description get方法绑定参数 自定义restful风格的域名
	 * @author 周杨
	 * @Time 2018年10月24日 下午2:29:57
	 */
	@RequestMapping(value="/testBean/{id}/{name}")
	public @ResponseBody Test testBean(@PathVariable int id, @PathVariable String name) {
		logger.debug("testBean,this is a log test, debug");
		logger.info("testBean,this is a log test, info");
		Test test=new Test();
		test.setId(id);
		test.setName(name);
		test.setDate(new Date());
		return test;
	}
	
	@RequestMapping(value = "/testGet")
	public @ResponseBody HashMap<String, Object> testGet(@RequestParam String name) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("title", "hello world");
		map.put("name", name);
		return map;
	}
	
	/**
	 * @description 使用freemark模板引擎 不要加上@ResponseBody注解 自动匹配templates里模板
	 * @author 周杨
	 * @Time 2018年10月24日 下午2:36:48
	 */
	@RequestMapping(value = "/testFreemark")
	public String testFreemark(ModelMap modelMap) {
		modelMap.put("title", "这就是title");
		return "freemark";//不要加上/ linux无法解析
	}
	
	/**
	 * @description 整个流程 controller- service- dao - ftl
	 * @author 周杨
	 * @Time 2018年10月24日 下午3:48:59
	 */
	@RequestMapping(value = "/testController")
	public @ResponseBody Test testController(@RequestParam int id){
		return this.testService.findById(id);
	}
	
	@RequestMapping(value="/mail")
	public @ResponseBody String mail(String email){
		this.mailComponent.sendMail(email);
		return "success";
	}
	
	@RequestMapping(value="/bean"/*,method=RequestMethod.POST*/)
	public @ResponseBody String bean(Test test){
		System.out.println(test);
		return "success";
	}
}
